package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.sys.bean.Field;
import com.fzrskj.cloud.free.sys.bean.Param;
import com.fzrskj.cloud.free.sys.bean.Table;
import com.fzrskj.cloud.free.sys.dao.CDicDao;
import com.fzrskj.cloud.free.sys.dao.CParamDao;
import ms.core.object.CObject;

import java.util.List;

public class CDicUtils extends CObject{
	
	/**to query table dictionary by itype
	 * @param itype table type,0-dictionary; 1-system;... 
	 * @return Table list or null
	 * @author lms 2016.08.18
	 */
	public List<Table> getTableDic(int itype){
		CDicDao obj = new CDicDao();
		List<Table> tbs = obj.getTalbeDic(itype);
		this.setErrCode(obj.getErrCode());
		obj.dispose();
		return tbs;
	}

	/**to query field dictionary by table code
	 * @param code table code
	 * @return null for error or not found, otherwise return List<Field>
	 * @author lms 2016.08.18
	 */
	public List<Field> getFieldDic(String code){
		CDicDao obj = new CDicDao();
		List<Field> fls = obj.getFieldDic(code);
		this.setErrCode(obj.getErrCode());
		obj.dispose();
		return fls;
	}

	/**to lock table field show width 
	 * @param flds to update Field list
	 * @return count of updated
	 * @author lms 2016.08.18
	 */
	public int lockFieldDic(List<Field> flds){
		CDicDao obj = new CDicDao();
		int n = obj.lockFieldDic(flds);
		obj.dispose();
		return n;
	}

	/**to query system parameter
	 * @param code
	 * @return null for failure or not exists, otherwise return Param
	 * @author lms 2016.09.08
	 */
	public Param getParam(String code){
		CParamDao obj = new CParamDao();
		Param ps = obj.queryParam(code);
		this.setErrCode(obj.getErrCode());
		obj.dispose();
		return ps;
	}

	/**to query system parameter
	 * @param code table code
	 * @param type table type
	 * @return null for failure or not exists, otherwise return Param list
	 * @author lms 2016.08.18
	 */
	public List<Param> getParam(String code, int type){
		CParamDao obj = new CParamDao();
		List<Param> ps = obj.queryParam(type,code);
		this.setErrCode(obj.getErrCode());
		obj.dispose();
		return ps;
	}

	/**to set system parameter
	 * @param params
	 * @return rows effected
	 * @author lms 2016.08.18
	 */
	public int setParam(List<Param> params){
		int n = 0;
		CParamDao obj = new CParamDao();
		for(Param pm: params){
			if (obj.modParam(pm.getParamName(), pm.getParamValue())>0) n++;
		}
		obj.dispose();
		return n;
	}
}
